
from rest_framework.views import APIView
from datetime import date, timedelta
from users.models import User
from rest_framework.response import Response
from rest_framework.generics import GenericAPIView
from .serializers import UserGoodsVisiCountSerializer
from goods.models import GoodCategoryVisit


#总用户
class UserTotalCountView(APIView):
    #1.获取总用户
    def get(self,request):
        #包括管理员用户
        count=User.objects.count()
        #普通用户
        # count=User.objects.filter(is_superuser=False,is_staff=False).count()

        return Response({"count":count})
#日增用户
class UserIncrementView(APIView):
    def get(self,request):
        today=date.today()
        count=User.objects.filter(date_joined__gte=today).count()

        return Response({"count":count})
#日活跃用户
class UserDayActiveView(APIView):
    def get(self,request):
        today=date.today()
        count=User.objects.filter(last_login__gte=today).count()

        return Response({'count':count})

#日下单用户
class UserDayOrdersView(APIView):
    def get(self,request):
        today=date.today()
        count=User.objects.filter(orderinfo__create_time__gte=today).count()

        return Response({'count':count})

#月新增用户
class UserMonthIncrementView(APIView):
    def get(self,request):
        today=date.today()

        old_today=today-timedelta(days=29)

        moth_list=[]

        for i in range(0,30):
            #获取i所在的当天的日期
            now_date=old_today+timedelta(days=i)#i是从0开始的

            #获取i下一天日期
            next_date=old_today+timedelta(days=i+1)

            #获取过去30天,每天的新增用户
            count=User.objects.filter(date_joined__gte=now_date,date_joined__lte=next_date).count()

            moth_list.append({
                "date":now_date,
                "count":count
            })
        return Response(moth_list)

#日分类访问量
class UseGoodsVisitCountView(GenericAPIView):
    serializer_class = UserGoodsVisiCountSerializer

    def get(self,request):
        #1.获取当天时间
        today=date.today()
        goods_visit_count=GoodCategoryVisit.objects.filter(date__gte=today).all()
        #2.获取序列化器
        serializers=self.get_serializer(instance=goods_visit_count,many=True)

        #3.返回响应
        return Response(serializers.data)


